#include "contactswindow.h"
#include "contactdelegate.h"
#include "contactsqlmodel.h"
#include "contactform.h"
#include "initdb.h"

//Make flickable
#include "../flickcharm/flickcharm.h"

#include <QtSql>

ContactsWindow::ContactsWindow()
{
    ui.setupUi(this);

    ui.widget->setHidden(true);
    if (!QSqlDatabase::drivers().contains("QSQLITE"))
        QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");

    // initialize the database
    QSqlError err = initDb();
    if (err.type() != QSqlError::NoError) {
        QMessageBox::critical(this, "Unable to initialize Database",
                "Error initializing database: " + err.text());
        return;
    }

    model = new ContactSqlModel();
    mapper = new QDataWidgetMapper;
    mapper->setModel(model);
    mapper->setItemDelegate(new ContactDelegate(mapper));
    mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    mapper->addMapping(ui.widget->nameLine, model->fieldIndex("name"));
    mapper->addMapping(ui.widget->emailLine, model->fieldIndex("email"));
    mapper->addMapping(ui.widget->numberLine, model->fieldIndex("number"));

    // Set the relations to the other database tables
   // model->setRelation(authorIdx, QSqlRelation("authors", "id", "name"));
    //model->setRelation(genreIdx, QSqlRelation("genres", "id", "name"));

    ui.listView->setModel(model);
    contactDelegate = new ContactDelegate(ui.listView);
    ui.listView->setItemDelegate(contactDelegate);
    ui.listView->setModelColumn(model->fieldIndex("name"));

    //Make list flickable. See: http://labs.trolltech.com/blogs/2008/11/15/flick-list-or-kinetic-scrolling/
    //FlickCharm flickcharm;
    //flickcharm.activateOn(ui.listView);

    connect(ui.listView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            this, SLOT(setCurrentIndex(QModelIndex)));
    connect(ui.listView, SIGNAL(clicked(QModelIndex)),
            this, SLOT(contactActivated(QModelIndex)));
    connect(ui.listView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            mapper, SLOT(setCurrentModelIndex(QModelIndex)));
    connect(ui.widget, SIGNAL(accept()),
            mapper,   SLOT(submit()));
    connect(ui.widget, SIGNAL(accept()),
            this,   SLOT(contactClosed()));
    connect(ui.widget, SIGNAL(reject()),
            this,   SLOT(contactClosed()));

}
void ContactsWindow::contactActivated(const QModelIndex &index) {
     ui.widget->setVisible(true);
     ui.listView->scrollTo(index);
}

/*
void ContactsWindow::contactChanged() {
     ui.widget->setHidden(true);
     //ui.listView->scrollTo(index);
}
*/

void ContactsWindow::contactClosed() {
     ui.widget->setHidden(true);
     //ui.listView->scrollTo(index);
}

void ContactsWindow::setCurrentIndex(const QModelIndex &index) {

    if(!index.isValid())
        return;
    ui.widget->setHidden(true);
    ui.deleteButton->setEnabled(true);
    ui.listView->scrollTo(index);
}


void ContactsWindow::on_filterEdit_editingFinished()
{
    if(!ui.filterEdit->text().isEmpty()) {
        model->setFilter("name LIKE '"+ui.filterEdit->text()+"%';");
        ui.clearButton->setEnabled(true);
    } else if(!model->filter().isEmpty()) {
        model->setFilter("");
        ui.clearButton->setEnabled(false);
    }
}

void ContactsWindow::on_clearButton_clicked() {
    model->setFilter("");
    ui.filterEdit->clear();
}

void ContactsWindow::on_deleteButton_clicked() {
    model->removeRow(ui.listView->currentIndex().row());
}
